﻿//==============================================================================
// File: RcpStatusMonitor.cs
// Created: 2010-05-28
// Author: Arkadiusz Świerczek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using WpfRcp.Core.Services.Status;

namespace WpfRcp.Services.Status
{
    public class RcpStatusMonitor : SimpleRcpStatusMonitor, IRcpStatusMonitor
    {
        private double m_TotalWork;

        private double m_WorkDone;

        private readonly ObservableCollection<IRcpStatusMonitor> m_SubtaskMonitors = new ObservableCollection<IRcpStatusMonitor>();

        public event EventHandler WorkedChanged;

        public RcpStatusMonitor()
        {
            TotalWork = 1.0;
        }

        public void BeginTask(double totalWork)
        {
            if(totalWork < 0)
            {
                throw new ArgumentException("Total work must be greater or equal to zero.");
            }

            TotalWork = totalWork;
        }

        public void Worked(double work)
        {
            if (WorkDone + work > TotalWork)
            {
                WorkDone = TotalWork;
            }
            else
            {
                WorkDone += work;
            }
        }

        public IRcpStatusMonitor NewSubtaskMonitor(double totalSubWork)
        {
            if(totalSubWork < 0)
            {
                throw new ArgumentException("Total subwork must be greater or equal to zero.");
            }

            var monitor = new RcpStatusMonitor();
            
            m_SubtaskMonitors.Add(monitor);
            monitor.Disposed += (sender, e) => m_SubtaskMonitors.Remove(monitor);

            double lastWorkDone = 0;
            monitor.WorkedChanged += (sender, e) =>
            {
                var currentWorkDone = totalSubWork * monitor.WorkDone / monitor.TotalWork;
                WorkDone += currentWorkDone - lastWorkDone;
                lastWorkDone = currentWorkDone;
            };

            return monitor;
        }

        public double TotalWork
        {
            get { return m_TotalWork; }
            private set
            {
                m_TotalWork = value;
                OnPropertyChanged<RcpStatusMonitor>(t => t.TotalWork);
            }
        }

        public double WorkDone
        {
            get { return m_WorkDone; }
            private set
            {
                m_WorkDone = value;
                OnPropertyChanged<RcpStatusMonitor>(t => t.WorkDone);
                if (WorkedChanged != null)
                {
                    WorkedChanged(this, EventArgs.Empty);
                }
            }
        }

        public IEnumerable<IRcpStatusMonitor> SubtaskMonitors
        {
            get { return m_SubtaskMonitors; }
        }
    }
}